<?php
namespace app\admin\model;
use think\Model;
use think\Db;
class Question extends Model
{
	/*查询科目，keyid=1 把id作为键值*/
	function subjectList($keyid=0){
		$subject_list = Db::name('subject')->order('subject_sort')->select();
		foreach($subject_list as $k=>$v){
			$subjectList[$v['subjectid']] = $v;
		}

		return $subjectList;
	}
	/*查询题型，keyid=1 把id作为键值*/
	function qtypeList($keyid=0){
		$qtype_list = Db::name('qtype')->order('qtype_sort')->select();
		if($keyid == 1){
			foreach($qtype_list as $k=>$v){
				$qtypeList[$v['qtypeid']] = $v;
			}
		}else{
			$qtypeList = $qtype_list;
		}

		return $qtypeList;
	}

	function pageQuestion(){
		$list = array();
        $p = empty($_GET['p']) ? 1 : $_GET['p'];
        $size = empty($_REQUEST['size']) ? 15 : $_REQUEST['size'];

        $count = Db::name('question')->where($where)->count();// 查询满足要求的总记录数

        $list = Db::name('question')
			->alias('q')
			->join('subject s','q.subjectid = s.subjectid','left')
			->join('qtype qt','q.qtypeid = qt.qtypeid','left')
			->field('q.*,s.subject_name,qt.qtype_name')
			->where($where)
			->order('questionid desc')
			->paginate($size,$count,['query'=>request()->param()])
			->each(function($item,$key){
				return $item;
			});
        $page = $list->render();

        $data['count'] = $count;
        $data['list'] = $list;
        $data['page'] = $page;
        return $data;
	}
	function questionData($p=1,$size=15,$where=''){
		$list = array();
//		$p = empty($_GET['p']) ? 1 : $_GET['p'];
//		$size = empty($_REQUEST['size']) ? 15 : $_REQUEST['size'];
//		dump($where);exit;
		$count = Db::name('question')->alias('q')->where($where)->count();// 查询满足要求的总记录数

		$list = Db::name('question')
			->alias('q')
			->join('subject s','q.subjectid = s.subjectid','left')
			->join('qtype qt','q.qtypeid = qt.qtypeid','left')
			->join('week w','q.weekid = w.weekid','left')
			->field('q.*,s.subject_name,qt.qtype_name,w.week_name')
			->where($where)
			->order('questionid desc')
			->paginate($size,$count,['query'=>request()->param()])
			->each(function($item,$key){
				return $item;
			});
		$page = $list->render();

		$data['count'] = $count;
		$data['list'] = $list;
		$data['page'] = $page;
		return $data;
	}

	function getQuestion($id){
		$data = Question::where('questionid',$id)->find();
		$data['questionSelect'] = json_decode($data['question_select'],true);
		//1单选，2多选，3判断，4填空
		$data['question_answer_radio'] = '';
		$data['question_answer_checked'] = [];
		$data['question_answer_judge'] = '';
		$data['question_answer_fill'] = '';
		if($data['qtypeid'] == 1){
			$data['question_answer_radio'] = $data['question_answer'];
		}elseif($data['qtypeid'] == 2){
			$data['question_answer_checked'] = str_split($data['question_answer']);
		}elseif($data['qtypeid'] == 3){
			$data['question_answer_judge'] = $data['question_answer'];
		}elseif($data['qtypeid'] == 4){
			$data['question_answer_fill'] = $data['question_answer'];
		}
		return $data;
	}

	function questionDel($id){
		if($id > 0){
			$data['is_del'] = 1;
			$res = Question::where('questionid='.$id)->update($data);
			if($res !== false){
				return true;
			}else{
				return false;
			}
		}else{
			exitjson(0,'试题ID非法');
		}
	}


}